using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;


namespace AsterroidX
{
    public partial class Bullet : ViewController
    {
        public Vector2 Direction;

        public bool PowerUpRepetScreen;

        private GameModel mGameModel;


        public bool Big;

        public int Damage = 1;


        public bool RotatePlayer { get; set; }


        public bool Tornado { get; set; }

        private int mRotatePlayerFrames = 0;

        private void Awake()
        {
            mGameModel = this.GetModel<GameModel>();

            if (Tornado)           
            mGameModel.TornadoBulletCount++;

            mGameModel.BulletCount++;
        }

        private void Start()
        {
          

            UpdateBulletSize();

            if (RotatePlayer)
            {
                mRotatePlayerFrames = 0;
            }



            ColliderBig.OnTriggerEnter2DEvent(OnTriggerEnter2D).UnRegisterWhenGameObjectDestroyed(gameObject);
            ColliderSmall.OnTriggerEnter2DEvent(OnTriggerEnter2D).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        private void FixedUpdate()
        {
            if (!mGameModel.GameRunning) return;

            if (RotatePlayer)
            {
                Direction = (Direction.ToAngle() + Mathf.Clamp(360 - mRotatePlayerFrames, -90, 360) * Time.fixedDeltaTime)
                    .AngleToDirection2D();
                mRotatePlayerFrames++;
                if(mGameModel.BulletCount>Config.MaxBulletCount)
                {
                    this.SendCommand(new CacheBulletCmd(this));
                }
            }


            if (Tornado)
            {
                Direction = (Direction.ToAngle() + 360 * Time.fixedDeltaTime).AngleToDirection2D();
                mGameModel.TornadoCenter = this.Position2D();
            }

            transform.Translate(Direction * Time.fixedDeltaTime * 10);

            if (PowerUpRepetScreen)
            {
                ScreenHelper.RepeatScreen(transform, 0.5f, 0.5f);
            }

            else if (ScreenHelper.OutScreen(transform, 0.5f, 0.5f))
            {

                this.DestroyGameObj();

            }
        }
        private void OnDestroy()
        {
            if (Tornado)
                mGameModel.TornadoBulletCount--;

            mGameModel.BulletCount--;
        }


        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.gameObject.name.StartsWith("UFO"))
            {

                var ufo = collision.GetComponent<UFO>();

                Damage = ufo.Hurt(Damage);
                UpdateBulletSize();
                if (Damage <= 0)
                {
                    this.DestroyGameObj();
                }


            }

            else if (collision.gameObject.name.StartsWith("Rock"))
            {
                this.SendCommand(new RockDeadCmd());

                TypeEventSystem.Global.Send(new RockDeathEvent(this.transform));


                var rock = collision.GetComponent<Rock>();

                rock.PlaySfxRockDestroy();

                this.SendCommand(new AddScoreCmd(50));

                this.SendCommand(new GenerateScoreTextCmd(collision.Position2D() + Vector2.up * 0.5f, 50.ToString(), 0.1f));

                if (rock.Big)
                {
                    rock.SmallSize();
                    var rock2 = Instantiate(rock);
                    rock2.transform.Position(rock.transform.position);
                    rock2.GetComponent<Rock>().SmallSize();

                    CameraController.BigShake();
                }
                else if (mGameModel.RockCount< mGameModel.MaxRockCount)
                {
                    rock.Dead();
                    rock.BigSize();

                    if (RandomUtility.Choose(true, false))
                    {
                        rock.PositionX(ScreenHelper.LeftBottomScreenPos.x - rock.SpriteSize.x * 0.5f);
                    }
                    else
                    {
                        rock.PositionX(ScreenHelper.RightTopScreenPos.x + rock.SpriteSize.x * 0.5f);
                    }
                    rock.PositionY(Random.Range(ScreenHelper.LeftBottomScreenPos.y, ScreenHelper.RightTopScreenPos.y));
                    rock.RandomDirection();
                    CameraController.SmallRockShake();
                }
                else
                {
                    Destroy(collision.gameObject);
                    CameraController.SmallRockShake();
                }
                Damage--;

                if (Damage == 1)
                {
                    UpdateBulletSize();
                }
                else if (Damage <= 0)
                {
                    Destroy(this.gameObject);
                }

            }

        }
        private void UpdateBulletSize()
        {
            if (Big)
            {
                BigBullet();
            }
            else
            {
                SmallBullet();
            }
        }
        private void SmallBullet()
        {
            if (Damage == 1)
            {
                Sprite.sprite = BulletSprite;
                Sprite.drawMode = SpriteDrawMode.Simple;
                Sprite.LocalScale(0.5f);
                ColliderSmall.Show();
                ColliderBig.Hide();
            }
            else if (Damage == 2)
            {
                Sprite.sprite = BulletDamageUpSprite;
                Sprite.drawMode = SpriteDrawMode.Simple;
                Sprite.LocalScale(0.5f);
                ColliderSmall.Show();
                ColliderBig.Hide();
            }

        }


        private void BigBullet()
        {

            if (Damage == 1)
            {
                Sprite.sprite = BulletDamageUpSprite;
                Sprite.drawMode = SpriteDrawMode.Sliced;
                Sprite.size = Vector2.one;
                Sprite.LocalScale(1f);
                ColliderSmall.Hide();
                ColliderBig.Show();
            }
            else if (Damage == 2)
            {
                Sprite.sprite = BulletDamageUpSprite;
                Sprite.drawMode = SpriteDrawMode.Sliced;
                Sprite.size = Vector2.one;
                Sprite.LocalScale(1f);
                ColliderSmall.Hide();
                ColliderBig.Show();
            }

        }
    }
}

